<template>
<view class="app-content">

	<SeachNav @search="searchKey"  @openPopup="openPopup"></SeachNav>
	<screen :filterMaskData.sync="filterMaskData"  @filterQuery="filterQuery"  @filterBtnCall="filterQuery" ref="screen"></screen>

	<counsel-list
		ref="counsel"
		v-if="!list.loading"
		:list="list"
		:counselData="counselData"
		@goDetail="goDetail"
		@pushIteam="pushIteam"
		@popularize="popularize">
	</counsel-list>
	<!--@toMaterialList="toMaterialList"-->

</view>
</template>

<script>
import SeachNav from '@/pages/components/SeachNav.vue'
import CounselList from '@/pages/getGuest/components/CounselList.vue'
import screen from '@/components/App_screen.vue'

import { getCaseSelectList, getCaseWinList } from '@/api/getGuest'

import { list } from '@/mixins'
export default {
	name: 'winCase',
	mixins: [list],
	components: {
		SeachNav,
		CounselList,
		screen
	},
	data() {
		return {
			form: {
				name: '',
				cat_id: ''
			},
			filterMaskData: [
				{
					title: '装修风格',
					// type: 2,
					key: 'case_style',
					list: []
				},
				{
					title: '标签',
					// type: 2,
					key: 'is_public',
					list: []
				},
				{
					title: '来源',
					// type: 2,
					key: 'source_type',
					list: []
				}
			],
			counselData: {
				title: '获客案例',
				shareType: 2,
				type: 'case',
				myKey: {
					id: 'case_id',
					images: 'image_url',
					title: 'case_name'
				}
			}
		}
	},
	onLoad(option) {
		this.option = option
		this.init()
	},
	// 下拉刷新
	 onPullDownRefresh() {
	    let that = this
		this.resetList()
		this.getCaseWin(this.option).then(res => {
			uni.stopPullDownRefresh()
		})
	},
	// 加载更多
	onReachBottom() {
		if (this.list.loading) {
			return
		}
		if (this.list.data.length < this.list.total) {
			this.getCaseWin(this.option).then(res => {
				uni.stopPullDownRefresh()
			})
		} else {
			uni.stopPullDownRefresh()
			uni.showToast({
				title: '没有更多了',
				duration: 2000,
				icon: 'none'
			})
		}
	},
	methods: {
	    // ===初始化===
	    init() {
			this.getCaseWin()
			this.getSelectList()
		},

		// ===搜索===
		searchKey(val) {
			this.form.name = val
			this.resetList()
			this.getCaseWin()
		},
		openPopup() {
			this.$refs.screen.openPopup()
		},
		// ===筛选===
		getSelectList() {
			let that = this
			getCaseSelectList().then((res) => {
				if (res.status === 'success') {
					that.filterMaskData[0].list = that.formatSelect(res.data.style_list)
					that.filterMaskData[1].list = that.formatSelect(res.data.type_list)
					that.filterMaskData[2].list = that.formatSelect(res.data.source_type_list)
				}
			})
		},
		// 筛选格式化
		formatSelect(data) {
			return data.map(i => ({
				name: i.style_name ? i.style_name : i.type_name ? i.type_name : i.type_name,
				status: false,
				id: i.style_id ? i.style_id : i.is_public ? i.is_public : i.source_type,
				type: i.is_public ? 'type_list' : i.source_type ? 'source_type' : ''
			}))
		},
		// ===重置筛选===
		filterQuery(val) {
			this.form = {
				...this.form,
				...val.form
			}
			this.resetList()
			this.getCaseWin()
		},

		// ===列表数据===
		getCaseWin() {
		    let that = this
			return getCaseWinList({
				owner_user_id: that.option.owner_user_id,
				page: that.list.page,
				limit: that.list.limit,
				...that.form
			}).then((res) => {
			    that.completes(res)
			})
		},

		// 朋友圈推广
		toMaterialList(data, type) {
			let param = this.$refs.counsel.forParams(data)
			let params = {
				...param,
				relevance_id: data.case_relevance_id,
				case_article_id: data.case_id,
				resource_type: 2,
				imgUrl: data.image_url,
				cat_name: '',
				from: 'caseOrArticle'
			}
			uni.navigateTo({
				url: `/pages/getGuest/shareList${this.commonJS.queryData(params)}`
			})
		},
		// ===推给团队===
		pushIteam(data, type) {
			let param = this.$refs.counsel.forParams(data)
			let params = {
				...param,
				imgUrl: param.images,
				cat_name: '',
				type: 2,
				from_origin: 1
			}
			// let params = {
			// 	...param,
			// 	status: '',
			// 	type: 2,
			// 	from_origin: 1
			// }
			// let returnUrl = encodeURIComponent(`${this.$constant.formal}/hkds/#/admin/guest/GeneralizeIteam?${this.commonJS.queryData(params)}`)
			// uni.navigateTo({
			// 	url: `/pages/page/webview/index?user_id=${this.owner_user_id}&returnUrl=${returnUrl}`
			// })
			console.log(params, '获客案例 --> 推给团队')
			uni.navigateTo({
				url: `/pages/getGuest/pushTeam${this.commonJS.queryData(params)}`
			})
		},
		// 推广获客
		popularize(val) {
			this.$nextTick(() => {
				this.$refs.counsel.$refs.sharePoster.showMask({
					id: val[this.counselData.myKey.id],
					url: true
				}, true)
			})
		},
		// 进入详情
		goDetail(val) {
			let query = {
				mainId: val.case_id,
				caseid: 'null',
				owner_user_id: this.option.owner_user_id
			}
			uni.navigateTo({
				url: `/pages/case/casedetail${this.commonJS.queryData(query)}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
</style>
